﻿using GameFramework;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
using TMPro;

namespace DTFramework.UI
{
    /// <summary>
    /// dui'hua'kuang
    /// </summary>
    public class NativeDialogForm : MonoBehaviour
    {
        [SerializeField] TextMeshProUGUI m_TextMessage;
        [SerializeField] Button m_BtnCancel;
        [SerializeField] Button m_BtnEnsure;

        private bool m_PauseGame = false;
        private object m_UserData = null;

        public void OnOpen(DialogParams dialogParams)
        {
            if (dialogParams == null)
            {
                Log.Warning("DialogParams is invalid.");
                return;
            }

            m_TextMessage.text = dialogParams.Message;

            m_PauseGame = dialogParams.PauseGame;
            RefreshPauseGame();

            m_UserData = dialogParams.UserData;

            RefreshConfirmText(dialogParams.ConfirmText);
            m_BtnEnsure.onClick.AddListener(() =>
            {
                Close();
                dialogParams.OnClickConfirm?.Invoke(m_UserData);
            });

            RefreshCancelText(dialogParams.CancelText);
            m_BtnCancel.onClick.AddListener(() =>
            {
                Close();
                dialogParams.OnClickCancel?.Invoke(m_UserData);
            }); 
        }

        private void Close()
        {
            if (m_PauseGame)
            {
                GameEntry.Base.ResumeGame();
            }
            m_TextMessage.text = string.Empty;
            m_PauseGame = false;
            m_UserData = null;

            RefreshConfirmText(string.Empty);
            m_BtnEnsure.onClick.RemoveAllListeners();

            RefreshCancelText(string.Empty);
            m_BtnCancel.onClick.RemoveAllListeners();
        }


        private void RefreshPauseGame()
        {
            if (m_PauseGame)
            {
                GameEntry.Base.PauseGame();
            }
        }

        private void RefreshConfirmText(string confirmText)
        {
            if (string.IsNullOrEmpty(confirmText))
            {
                m_BtnEnsure.gameObject.SetActive(false);
            }
            else
            {
                m_BtnEnsure.gameObject.SetActive(true);
                m_BtnEnsure.GetComponent<TextMeshProUGUI>().text = confirmText;
            }
        }

        private void RefreshCancelText(string cancelText)
        {
            if (string.IsNullOrEmpty(cancelText))
            {
                m_BtnCancel.gameObject.SetActive(false);
            }
            else
            {
                m_BtnCancel.gameObject.SetActive(true);
                m_BtnCancel.GetComponent<TextMeshProUGUI>().text = cancelText;
            }
        }
    }

    /// <summary>
    /// 对话框显示数据。
    /// </summary>
    public class DialogParams
    {
        /// <summary>
        /// 消息内容。
        /// </summary>
        public string Message
        {
            get;
            set;
        }

        /// <summary>
        /// 弹出窗口时是否暂停游戏。
        /// </summary>
        public bool PauseGame
        {
            get;
            set;
        }

        /// <summary>
        /// 确认按钮文本。
        /// </summary>
        public string ConfirmText
        {
            get;
            set;
        }

        /// <summary>
        /// 确定按钮回调。
        /// </summary>
        public GameFrameworkAction<object> OnClickConfirm
        {
            get;
            set;
        }

        /// <summary>
        /// 取消按钮文本。
        /// </summary>
        public string CancelText
        {
            get;
            set;
        }

        /// <summary>
        /// 取消按钮回调。
        /// </summary>
        public GameFrameworkAction<object> OnClickCancel
        {
            get;
            set;
        }
        /// <summary>
        /// 用户自定义数据。
        /// </summary>
        public string UserData
        {
            get;
            set;
        }
    }
}
